//
//  HypnoViewController.m
//  HypnoTime
//
//  Created by Xia Yong on 13-1-10.
//  Copyright (c) 2013年 Xia Yong. All rights reserved.
//

#import "HypnoViewController.h"
#import "HypnosisView.h"

@interface HypnoViewController () <UIAccelerometerDelegate>

- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
// 当方向感应器更新数据后发送该消息
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;

@end

@implementation HypnoViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    HypnosisView *hypnosisView = [[HypnosisView alloc] initWithFrame:CGRectZero];
    [hypnosisView setBackgroundColor:[UIColor whiteColor]];
    self.view = hypnosisView;

    //[self.hypnosisScreen setBackgroundColor:[UIColor purpleColor]];
}

// 当视图将要显示时设置UIAccelerometer的委托对象
- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"Monitoring accelerometer");
    
    // 获取UIAccelerometer对象
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    // 将时间间隔设置为0.1秒
    accelerometer.updateInterval = 0.1;
    accelerometer.delegate = self;
    // 设置第一响应对象
    [self.view becomeFirstResponder];
}

// 当视图移除后不在接受UIAccelerometer更新的数据
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [UIAccelerometer sharedAccelerometer].delegate = nil;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    NSLog(@"%f, %f, %f", acceleration.x, acceleration.y, acceleration.z);
    HypnosisView *hypnosisView = (HypnosisView *)self.view;
    hypnosisView.xShift = hypnosisView.xShift * 0.8 + acceleration.x * 2;
    hypnosisView.yShift = hypnosisView.yShift * 0.8 - acceleration.y * 2;
    [self.view setNeedsDisplay];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
